package com.erci.service.Impl;

import com.erci.entity.Category;
import com.erci.entity.VO.CategoryVO;
import com.erci.mapper.CategoryMapper;
import com.erci.result.PageResult;
import com.erci.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public PageResult queryList(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        Page<Category> pageList = categoryMapper.queryList(name);
        return new PageResult(pageList.getResult(), pageList.getTotal());
    }

    @Override
    public void add(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.add(category);
    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public void delete(Category category) {
        categoryMapper.deleteById(category.getId());
    }

    ///////////////////////////////////////////////////////////////////////////////////////

    @Override
    public List<CategoryVO> allCategory() {
        List<CategoryVO> categoryVOList = categoryMapper.allCategory();
        return categoryVOList;
    }

    @Override
    public List<String> allCategoryNames(List<String> ids) {
        List<String> list = categoryMapper.allCategoryNames(ids);
        return list;
    }
}
